package com.itboye.lottery.activity.user

import android.text.Editable
import android.text.TextWatcher
import android.view.View
import com.itboye.lottery.R
import com.itboye.lottery.alert.ByAlert
import com.itboye.lottery.base.BaseActivity
import com.itboye.lottery.presenter.UserPresenter
import com.itboye.lottery.retrofit.ResponseCallback
import kotlinx.android.synthetic.main.activity_ti_xian_detail.*

class TiXianDetailActivity : BaseActivity(), ResponseCallback {
    var amount = 0.0
    var userPresenter: UserPresenter = UserPresenter(this)
    var withdrawType = 1
    var accountType="alipay"//weixin,alipay,bank 3种
    override fun success(o: Any, requestSuccessType: String) {
        if (requestSuccessType == userPresenter.tixianShenQing_SUCCESS) {
            ByAlert.alert(o)
            finish()
        }
    }

    override fun fail(code: Int, msg: Any, requestErrorType: String) {
        if (requestErrorType == userPresenter.tixianShenQing_FAIL) {
            ByAlert.alert(msg)
        } else {

        }
    }


    override fun getLayoutId(): Int {
        return R.layout.activity_ti_xian_detail
    }

    var DECIMAL_DIGITS = 2
    var accountStatus = false//账号输入状态
    var amountStatus = false//金额输入状态
    var bankTypeStatus = false//银行卡开户行输入状态
    var nameStatus = false//开户姓名输入状态
    override fun initData() {
        amount = intent.getStringExtra("amount").toDouble()
        withdrawType = intent.getIntExtra("withdrawType", 0)//0 ：支付宝  1：银行卡
        when (withdrawType) {
            0 -> {
                tv_tixian_account.hint = "支付宝账号"
                rl_kaihuhang.visibility = View.GONE
                accountType="alipay"
            }
            1 -> {
                tv_tixian_account.hint = "银行卡账号"
                accountType="bank"
            }

            else -> {
            }
        }
        tv_avaliable_balance.text = "$amount"
        tv_tixian_jine.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(p0: Editable?) {

            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {
                System.out.println("onTextChanged》》》")
                amountStatus = if (s?.length!! > 0) {
                    s.toString().toDouble() > 0 && s.toString().toDouble() <= amount
                } else {
                    false
                }
                setBtnStatus()
            }
        })
        tv_tixian_account.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(p0: Editable?) {
            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {
                accountStatus = s?.length!! > 0
                setBtnStatus()
            }
        })
        tv_tixian_name.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(p0: Editable?) {
            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {
                nameStatus = s?.length!! > 0
                setBtnStatus()
            }
        })
        tv_tixian_bank_type.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(p0: Editable?) {
            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {
                bankTypeStatus = s?.length!! > 0
                setBtnStatus()
            }
        })
    }

    fun setBtnStatus() {
        if (accountStatus && amountStatus && nameStatus && bankTypeStatus && withdrawType == 1) {
            btn_tixian_sun.setBackgroundColor(resources.getColor(R.color.main_color))
            btn_tixian_sun.isEnabled = true
        } else if (accountStatus && amountStatus && nameStatus && withdrawType == 0) {
            btn_tixian_sun.setBackgroundColor(resources.getColor(R.color.main_color))
            btn_tixian_sun.isEnabled = true
        } else {
            btn_tixian_sun.isEnabled = false
            btn_tixian_sun.setBackgroundColor(resources.getColor(R.color.main_text_color))
        }

    }

    fun tixian_all(view: View) {
        tv_tixian_jine.setText(amount.toString())
    }

    fun tixian_sub(view: View) {
        var money = tv_tixian_jine.text.toString()
        if (money.isEmpty()) {
            ByAlert.alert("请输入金额")
            return
        }
        var account = tv_tixian_account.text//提现账号
        var name=tv_tixian_name.text//提现用户名
        var bankType=tv_tixian_bank_type.text//开户行

        userPresenter.tixianShenQing(money, account.toString(),accountType,name.toString(),bankType.toString())
    }

    override fun onClick(p0: View?) {

    }
}
